<template>
  <div class="sales">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>销售管理</span>
          <el-button type="primary" @click="handleAdd">添加销售</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="销售姓名">
          <el-input v-model="searchForm.name" placeholder="请输入销售姓名" />
        </el-form-item>
        <el-form-item label="小组">
          <el-select v-model="searchForm.group" placeholder="请选择小组">
            <el-option label="A组" value="A" />
            <el-option label="B组" value="B" />
            <el-option label="C组" value="C" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="salesList" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="销售姓名" width="120" />
        <el-table-column prop="group" label="小组" width="100" />
        <el-table-column prop="leader" label="组长" width="120" />
        <el-table-column prop="leads" label="线索量" width="100" />
        <el-table-column prop="visit" label="到店量" width="100" />
        <el-table-column prop="deal" label="成交量" width="100" />
        <el-table-column prop="rate" label="成交率" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === '在职' ? 'success' : 'info'">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="primary" link @click="handleViewDetails(row)">详情</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 添加/编辑销售对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="销售姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入销售姓名" />
        </el-form-item>
        <el-form-item label="小组" prop="group">
          <el-select v-model="form.group" placeholder="请选择小组">
            <el-option label="A组" value="A" />
            <el-option label="B组" value="B" />
            <el-option label="C组" value="C" />
          </el-select>
        </el-form-item>
        <el-form-item label="组长" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入组长姓名" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio value="在职">在职</el-radio>
            <el-radio value="离职">离职</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

const searchForm = reactive({
  name: '',
  group: ''
})

const salesList = ref([
  {
    id: 1,
    name: '张三',
    group: 'A组',
    leader: '李四',
    leads: 156,
    visit: 45,
    deal: 12,
    rate: '26.7%',
    status: '在职'
  },
  {
    id: 2,
    name: '王五',
    group: 'B组',
    leader: '赵六',
    leads: 142,
    visit: 38,
    deal: 10,
    rate: '26.3%',
    status: '在职'
  }
])

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const dialogVisible = ref(false)
const dialogTitle = ref('添加销售')
const formRef = ref(null)

const form = reactive({
  name: '',
  group: '',
  leader: '',
  status: '在职'
})

const rules = {
  name: [{ required: true, message: '请输入销售姓名', trigger: 'blur' }],
  group: [{ required: true, message: '请选择小组', trigger: 'change' }],
  leader: [{ required: true, message: '请输入组长姓名', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

const handleSearch = () => {
  // 实现搜索逻辑
  console.log('搜索条件：', searchForm)
}

const resetSearch = () => {
  searchForm.name = ''
  searchForm.group = ''
}

const handleAdd = () => {
  dialogTitle.value = '添加销售'
  dialogVisible.value = true
  Object.assign(form, {
    name: '',
    group: '',
    leader: '',
    status: '在职'
  })
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑销售'
  dialogVisible.value = true
  Object.assign(form, row)
}

const handleViewDetails = (row) => {
  router.push(`/sales/detail/${row.id}`)
}

const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该销售吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 实现删除逻辑
    ElMessage.success('删除成功')
  }).catch(() => {})
}

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 实现提交逻辑
      ElMessage.success('保存成功')
      dialogVisible.value = false
    }
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 实现分页逻辑
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 实现分页逻辑
}
</script>

<style scoped>
.sales {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 